import 'package:flutter/material.dart';
import '../widgets/platform/adaptive_card.dart';

/// 增强通知系统组件
class EnhancedNotification extends StatefulWidget {
  final int unreadCount;
  final ValueChanged<NotificationItem>? onNotificationTap;
  final VoidCallback? onClearAll;

  const EnhancedNotification({
    super.key,
    this.unreadCount = 0,
    this.onNotificationTap,
    this.onClearAll,
  });

  @override
  State<EnhancedNotification> createState() => _EnhancedNotificationState();
}

class _EnhancedNotificationState extends State<EnhancedNotification> {
  // 模拟通知数据
  final List<NotificationItem> _notifications = [
    NotificationItem(
      id: '1',
      title: '新需求提交',
      content: '用户张三提交了新的需求：用户行为分析报表开发',
      time: '5分钟前',
      isUnread: true,
      type: NotificationType.requirement,
    ),
    NotificationItem(
      id: '2',
      title: '任务状态更新',
      content: '任务"数据源核查"已完成',
      time: '1小时前',
      isUnread: true,
      type: NotificationType.task,
    ),
    NotificationItem(
      id: '3',
      title: '系统维护提醒',
      content: '系统将在今晚00:00-02:00进行维护',
      time: '3小时前',
      isUnread: false,
      type: NotificationType.system,
    ),
    NotificationItem(
      id: '4',
      title: '协作邀请',
      content: '李四邀请您参与需求评审会议',
      time: '1天前',
      isUnread: false,
      type: NotificationType.collaboration,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        IconButton(
          icon: const Icon(Icons.notifications_none),
          onPressed: _showNotificationPanel,
        ),
        if (widget.unreadCount > 0)
          Positioned(
            right: 8,
            top: 8,
            child: Container(
              width: 16,
              height: 16,
              decoration: const BoxDecoration(
                color: Colors.red,
                shape: BoxShape.circle,
              ),
              child: Center(
                child: Text(
                  widget.unreadCount > 99
                      ? '99+'
                      : widget.unreadCount.toString(),
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 10,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ),
      ],
    );
  }

  /// 显示通知面板
  void _showNotificationPanel() {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return Container(
          height: MediaQuery.of(context).size.height * 0.8,
          decoration: const BoxDecoration(
            borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
          ),
          child: Column(
            children: [
              // 顶部拖拽指示器
              Container(
                margin: const EdgeInsets.symmetric(vertical: 8),
                width: 40,
                height: 4,
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
              // 标题栏
              Padding(
                padding: const EdgeInsets.all(16),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '通知',
                      style: Theme.of(context).textTheme.titleLarge,
                    ),
                    if (_notifications.any((n) => n.isUnread))
                      TextButton(
                        onPressed: () {
                          // 标记所有为已读
                          setState(() {
                            for (var notification in _notifications) {
                              notification.isUnread = false;
                            }
                          });
                          Navigator.pop(context);
                        },
                        child: const SelectableText('全部标记为已读'),
                      ),
                  ],
                ),
              ),
              // 通知列表
              Expanded(
                child: _notifications.isEmpty
                    ? _buildEmptyState()
                    : ListView.builder(
                        itemCount: _notifications.length,
                        itemBuilder: (context, index) {
                          return _buildNotificationItem(_notifications[index]);
                        },
                      ),
              ),
              // 底部操作按钮
              if (_notifications.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.all(16),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButton(
                        onPressed: widget.onClearAll,
                        child: const SelectableText('清空所有通知'),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: const SelectableText('完成'),
                      ),
                    ],
                  ),
                ),
            ],
          ),
        );
      },
    );
  }

  /// 构建空状态
  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.notifications_none,
            size: 64,
            color: Colors.grey[400],
          ),
          const SizedBox(height: 16),
          const SelectableText(
            '暂无通知',
            style: TextStyle(
              fontSize: 16,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建通知项
  Widget _buildNotificationItem(NotificationItem notification) {
    return AdaptiveCard(
      child: ListTile(
        contentPadding: const EdgeInsets.all(12),
        leading: Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color:
                _getNotificationColor(notification.type).withValues(alpha: 0.1),
            borderRadius: BorderRadius.circular(20),
          ),
          child: Icon(
            _getNotificationIcon(notification.type),
            color: _getNotificationColor(notification.type),
            size: 20,
          ),
        ),
        title: SelectableText(
          notification.title,
          style: TextStyle(
            fontWeight:
                notification.isUnread ? FontWeight.bold : FontWeight.normal,
          ),
        ),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SelectableText(
              notification.content,
              style: const TextStyle(fontSize: 12),
              maxLines: 2,
              // overflow: TextOverflow.ellipsis, // SelectableText不支持overflow参数
            ),
            const SizedBox(height: 4),
            SelectableText(
              notification.time,
              style: TextStyle(
                fontSize: 10,
                color: notification.isUnread
                    ? Theme.of(context).primaryColor
                    : Colors.grey,
              ),
            ),
          ],
        ),
        trailing: notification.isUnread
            ? Container(
                width: 8,
                height: 8,
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                  shape: BoxShape.circle,
                ),
              )
            : null,
        onTap: () {
          // 标记为已读
          setState(() {
            notification.isUnread = false;
          });

          // 调用回调
          widget.onNotificationTap?.call(notification);

          // 关闭面板
          Navigator.pop(context);
        },
      ),
    );
  }

  /// 获取通知颜色
  Color _getNotificationColor(NotificationType type) {
    switch (type) {
      case NotificationType.requirement:
        return Colors.blue;
      case NotificationType.task:
        return Colors.green;
      case NotificationType.system:
        return Colors.orange;
      case NotificationType.collaboration:
        return Colors.purple;
      default:
        return Colors.grey;
    }
  }

  /// 获取通知图标
  IconData _getNotificationIcon(NotificationType type) {
    switch (type) {
      case NotificationType.requirement:
        return Icons.list_alt;
      case NotificationType.task:
        return Icons.assignment;
      case NotificationType.system:
        return Icons.settings;
      case NotificationType.collaboration:
        return Icons.group;
      default:
        return Icons.notifications;
    }
  }
}

/// 通知类型枚举
enum NotificationType {
  requirement,
  task,
  system,
  collaboration,
}

/// 通知项数据模型
class NotificationItem {
  final String id;
  final String title;
  final String content;
  final String time;
  bool isUnread;
  final NotificationType type;

  NotificationItem({
    required this.id,
    required this.title,
    required this.content,
    required this.time,
    required this.isUnread,
    required this.type,
  });
}
